package com.pxmeta.app.controller.game;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.party.entity.Association;
import com.pxmeta.party.entity.AssociationUser;
import com.pxmeta.party.entity.dto.BreakAnchorDTO;
import com.pxmeta.party.entity.dto.BreakAssociationDTO;
import com.pxmeta.party.entity.vo.AssociationDetailVO;
import com.pxmeta.party.service.IAssociationService;
import com.pxmeta.party.service.IAssociationUserService;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * <p>
 * 语聊公会 前端控制器
 * </p>
 *
 * @author wcl
 * @since 2023-02-05
 */
@RestController
@RequestMapping("/party/association")
@RequiredArgsConstructor
@Api(value = "公会模块",tags = "公会模块")
public class AssociationController {

    private final IAssociationService associationService;
    private final IAssociationUserService associationUserService;
    private final IUserService userService;


    @SaCheckLogin
    @ApiOperation(value = "获取公会排行")
    @GetMapping("/getRankingList")
    public AjaxResult getAssociationRanking(){
        List<Association> list = associationService.getRankingList();
        return AjaxResult.success(list);
    }

    @SaCheckLogin
    @ApiOperation(value = "获取公会详情")
    @GetMapping("/getDetail")
    public AjaxResult getAssociationDetail(@RequestParam Long associationId){
        Long userId = SecurityUtils.getUserId();
        AssociationDetailVO vo = associationService.getAssociationDetail(userId,associationId);
        return AjaxResult.success(vo);
    }


    @SaCheckLogin
    @ApiOperation(value = "解散公会")
    @DeleteMapping("/dissolutionAssociation")
    public AjaxResult getAssociationDetail(){
        Long userId = SecurityUtils.getUserId();
        associationService.dissolutionAssociation(userId);
        return AjaxResult.success();
    }

    @SaCheckLogin
    @ApiOperation(value = "签约公会")
    @GetMapping("/signingAssociation")
    public AjaxResult joinAssociation(@RequestParam Long associationId){

        Integer signingTime = 6; //默认签约6个月。
        BigDecimal finalRate = BigDecimal.valueOf(30 / 100).setScale(2, RoundingMode.HALF_UP);
        Long userId = SecurityUtils.getUserId();
        associationUserService.signing(userId, associationId, signingTime, finalRate);
        return AjaxResult.success("申请成功");
    }

    @SaCheckLogin
    @ApiOperation("解约主播")
    @PutMapping("/breakAnchor")
    public AjaxResult breakAnchor(@RequestBody BreakAnchorDTO dto){
        Long anchorId = dto.getAnchorId();
        Long userId = SecurityUtils.getUserId();
        associationService.breakAnchor(userId, anchorId, dto.getAssociationId());
        return AjaxResult.success();
    }


    @SaCheckLogin
    @ApiOperation("解约公会")
    @PutMapping("/breakAssociation")
    public AjaxResult breakAssociation(@RequestBody BreakAssociationDTO dto){
        Long userId = SecurityUtils.getUserId();
        associationService.breakAssociation(userId, dto.getAssociationId());
        return AjaxResult.success();
    }


    @SaCheckLogin
    @ApiOperation("查看签约申请列表")
    @GetMapping("/getSigningList")
    public AjaxResult getSigningList(@RequestParam(required = false) Integer status,
                                     @RequestParam Integer page,
                                     @RequestParam Integer size){

        Long userId = SecurityUtils.getUserId();
        User user = userService.getById(userId);
        Long associationId = user.getAssociationId();

        IPage<AssociationUser> list = associationUserService.getSigningList(associationId, status, page, size);
        return AjaxResult.success(list.getRecords());
    }


    @SaCheckLogin
    @ApiOperation("审批")
    @GetMapping("/approval")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "associationId", value = "公会id"),
                    @ApiImplicitParam(name = "userId", value = "申请人id"),
                    @ApiImplicitParam(name = "status", value = "状态，-1拒绝，1同意"),
            }
    )
    public AjaxResult approval(@RequestParam Long associationId,
                               @RequestParam Long userId,
                               @RequestParam Integer status){
        associationUserService.approval(associationId, userId, status);
        return AjaxResult.success();
    }



}
